package com.gragra.evaluateFunctionWords.tests;
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashSet;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import com.gragra.evaluateFunctionWords.EvaluateFunctionsWithMapping;
import com.gragra.parseOptions.CommandLineException;
import com.gragra.parseOptions.Parser;
/**
 * 
 * @author a2
 * 28.04.2013
 */
public class TestEvaluateFunctionsWithMapping
{
	/**
	 * 
	 */
	private EvaluateFunctionsWithMapping efm;
	/**
	 * 
	 */
	private BufferedReader br;
	@Before
	public void setUp() throws Exception
	{
		efm = new EvaluateFunctionsWithMapping();
		StringReader sr = new StringReader("The|D cat|NN is|AUX on|PP the|D mat.|NN\nThe|D stain|NN is|AUX on|PP the|D carpet.|Noun\nThe|D Tom|PN is|AUX on|PP the|D table.|NN");
		br = new BufferedReader(sr);
	}
	@Test
	public void testGetFrameSize() throws CommandLineException, IOException
	{
		Parser pr = new Parser();
		efm.addNoRelevantOptions(pr);
		assertEquals(3,efm.getFrameSize());
		pr.parse(new String [] {"-fS","5","-pO"});
		StringWriter sr = new StringWriter();
		efm.writeOutFrames(pr, br, new BufferedWriter(sr));
		br.close();
		assertEquals(5,efm.getFrameSize());
	}
	@Test
	public void testSetMicroAverage() throws CommandLineException, IOException
	{
		Parser pr = new Parser();
		efm.addNoRelevantOptions(pr);
		assertTrue(efm.isMicroAverage());
		pr.parse(new String [] {"-fS","3","-pO","-mA","-nW","3"});
		StringWriter sr = new StringWriter();
		efm.writeOutWords(pr, br, new BufferedWriter(sr));
		br.close();
		assertFalse(efm.isMicroAverage());
	}
	@Test
	public void testSetPostMarker() throws CommandLineException, IOException
	{
		Parser pr = new Parser();
		efm.addNoRelevantOptions(pr);
		assertTrue(efm.isMicroAverage());
		assertEquals(1,efm.getPostMarker());
		pr.parse(new String [] {"-fS","3","-pO","-mA","-nW","2","-postM","0"});
		StringWriter sr = new StringWriter();
		efm.writeOutWords(pr, br, new BufferedWriter(sr));
		br.close();
		assertFalse(efm.isMicroAverage());
		assertEquals(0,efm.getPostMarker());
	}
	@Test
	public void testSetPreMarker() throws CommandLineException, IOException
	{
		Parser pr = new Parser();
		efm.addNoRelevantOptions(pr);
		assertTrue(efm.isMicroAverage());
		assertEquals(1,efm.getPostMarker());
		assertEquals(1,efm.getPreMarker());
		pr.parse(new String [] {"-fS","3","-pO","-mA","-nW","2","-postM","0","-preM","2"});
		StringWriter sr = new StringWriter();
		efm.writeOutWords(pr, br, new BufferedWriter(sr));
		br.close();
		assertFalse(efm.isMicroAverage());
		assertEquals(0,efm.getPostMarker());
		assertEquals(2,efm.getPreMarker());
	}
	@Test
	public void testIsToLowerCase() throws CommandLineException, IOException
	{
		Parser pr = new Parser();
		efm.addNoRelevantOptions(pr);
		assertTrue(efm.isMicroAverage());
		assertTrue(efm.isToLowerCase());
		pr.parse(new String [] {"-fS","3","-pO","-mA","-nW","4","-kU"});
		StringWriter sr = new StringWriter();
		efm.writeOutWords(pr, br, new BufferedWriter(sr));
		br.close();
		assertFalse(efm.isMicroAverage());
		assertFalse(efm.isToLowerCase());
	}
	@Test
	public void testEvaluate() throws CommandLineException, IOException
	{
		Parser pr = new Parser();
		efm.addOptions(pr);
		pr.parse(new String [] {"-fS","3","-pO","-nW","4","-kU","-rT","D","AUX","PP"});
		double[] arr = efm.evaluate(pr, br);
		assertEquals(0.75,arr[0],0.00001);
		assertEquals(0.75,arr[1],0.00001);
		assertEquals(0.75,arr[2],0.00001);
	}
	@Test
	public void testWriteOutWords() throws CommandLineException, IOException
	{
		Parser pr = new Parser();
		efm.addNoRelevantOptions(pr);
		pr.parse(new String [] {"-fS","3","-pO","-mA","-nW","3","-nF","5"});
		StringWriter sr = new StringWriter();
		efm.writeOutWords(pr, br, new BufferedWriter(sr));
		br.close();
		String[] parts = sr.toString().split("\n");
		assertEquals(parts.length,3);
		Set<String> result = new HashSet<String>();
		for(String s : parts)
		{result.add(s.trim());}
		assertTrue(result.contains("the"));
		assertTrue(result.contains("is"));
		assertTrue(result.contains("."));
	}
	@Test
	public void testWriteOutFrames() throws IOException, CommandLineException
	{
		Parser pr = new Parser();
		efm.addNoRelevantOptions(pr);
		pr.parse(new String [] {"-fS","3","-pO","-mA","-nW","3","-nF","5"});
		StringWriter sr = new StringWriter();
		efm.writeOutFrames(pr, br, new BufferedWriter(sr));
		br.close();
		String[] parts = sr.toString().split("\n");
		assertEquals(parts.length,5);
		assertEquals("< on _ carpet >",parts[0].trim());
		assertEquals("< table _ BORDERMARKER >",parts[3].trim());
	}
}